package com.xmzs.sd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmzs.common.core.utils.MapstructUtils;
import com.xmzs.common.core.utils.StringUtils;
import com.xmzs.common.mybatis.core.page.PageQuery;
import com.xmzs.common.mybatis.core.page.TableDataInfo;
import com.xmzs.sd.domain.SysFileInfo;
import com.xmzs.sd.domain.bo.SysFileInfoBo;
import com.xmzs.sd.domain.vo.SysFileInfoVo;
import com.xmzs.sd.mapper.SysFileInfoMapper;
import com.xmzs.sd.service.ISysFileInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;

import java.util.List;
import java.util.Map;

/**
 * 文件记录Service业务层处理
 *
 * @author Lion Li
 * @date 2023-07-15
 */
@RequiredArgsConstructor
@Service
public class SysFileInfoServiceImpl implements ISysFileInfoService {
    private final SysFileInfoMapper baseMapper;

    /**
     * 查询文件记录
     */
    @Override
    public SysFileInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件记录列表
     */
    @Override
    public TableDataInfo<SysFileInfoVo> queryPageList(SysFileInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysFileInfo> lqw = buildQueryWrapper(bo);
        Page<SysFileInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件记录列表
     */
    @Override
    public List<SysFileInfoVo> queryList(SysFileInfoBo bo) {
        LambdaQueryWrapper<SysFileInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysFileInfo> buildQueryWrapper(SysFileInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysFileInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysFileInfo::getUrl, bo.getUrl());
        lqw.eq(bo.getSize() != null, SysFileInfo::getSize, bo.getSize());
        lqw.like(StringUtils.isNotBlank(bo.getFilename()), SysFileInfo::getFilename, bo.getFilename());
        lqw.like(StringUtils.isNotBlank(bo.getOriginalFilename()), SysFileInfo::getOriginalFilename, bo.getOriginalFilename());
        lqw.eq(StringUtils.isNotBlank(bo.getBasePath()), SysFileInfo::getBasePath, bo.getBasePath());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), SysFileInfo::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getExt()), SysFileInfo::getExt, bo.getExt());
        lqw.eq(bo.getUserId()!=null, SysFileInfo::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), SysFileInfo::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr()), SysFileInfo::getAttr, bo.getAttr());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateIp()), SysFileInfo::getUpdateIp, bo.getUpdateIp());
        return lqw;
    }

    /**
     * 新增文件记录
     */
    @Override
    public Boolean insertByBo(SysFileInfoBo bo) {
        SysFileInfo add = MapstructUtils.convert(bo, SysFileInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件记录
     */
    @Override
    public Boolean updateByBo(SysFileInfoBo bo) {
        SysFileInfo update = MapstructUtils.convert(bo, SysFileInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysFileInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文件记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
